﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RVBike
{
    class MultiLanguage : Singleton<MultiLanguage>
    {
        private List<string> _languages = new List<string>();

        public List<string> AvailableLanguages
        {
            get
            {
                return _languages;
            }
        }

        public string Locale { get; set; }

        private Dictionary<string, string> _translate;

        public MultiLanguage()
        {
            _translate = new Dictionary<string,string>();

            if (!Directory.Exists(PathUtility.Instance().LanguagesDir))
            {
                string error = String.Format("Languages folder \"{0}\" not exist", PathUtility.Instance().LanguagesDir);
                Logger.Instance().Fatal(error);
                throw new System.ArgumentException(error);
            }

            ScanLanguagesFolder(PathUtility.Instance().LanguagesDir);
        }

        private void ScanLanguagesFolder(string path)
        {
            Logger.Instance().Info("Start scanning languages folder ...");
            FileInfo localeFile = null;
            foreach (FileInfo fi in (new DirectoryInfo(path)).GetFiles())
            {
                Logger.Instance().Info(String.Format("Find language translation file : {0}", fi.Name));

                // Find locale file we want!!
                if (Path.GetFileNameWithoutExtension(fi.Name) == UserSetting.Instance().Locale)
                {
                    localeFile = fi;
                }

                _languages.Add(Path.GetFileNameWithoutExtension(fi.Name));
            }

            if (localeFile == null)
            {
                string error = String.Format("Language file \"{0}.txt\" not exist", UserSetting.Instance().Locale);
                Logger.Instance().Fatal(error);
                throw new System.ArgumentException(error);
            }

            Logger.Instance().Info(String.Format("Load language translation file : {0}", localeFile.Name));
            LoadLocaleFile(localeFile.FullName);

            Logger.Instance().Info("Finish scanning languages folder ...");
        }

        private void LoadLocaleFile(string localeFile)
        {
            // Clean old translation
            _translate.Clear();

            // Read line by line for $(English_String) = $(Local_String)
            using (StreamReader file = new StreamReader(localeFile))
            {
                string line;
                char[] delimiter = { '=' };
                while ((line = file.ReadLine()) != null)
                {
                    string[] words = line.Split(delimiter);
                    _translate[words[0]] = words[1];
                }
            }
        }

        public string Translate(string str)
        {
            if (_translate.ContainsKey(str))
            {
                return _translate[str];
            }
            else
            {
                return str;
            }
        }
    }
}